{% extends "layout/content.twig" %}

{% block main %}
    <div class="layui-card">
        <div class="layui-card-header">
            权限列表
        </div>
        <div class="layui-card-body">
            <form class="layui-form">
                {% for key,rule in rules %}
                    <div>
                        <fieldset class="layui-elem-field layui-field-title">
                            <legend>{{ rule.title }}</legend>
                        </fieldset>
                        <div class="plr30 ptb10 mtb10">
                            <div class="layui-row">
                                {% for item in rule.paths %}
                                    <div class="layui-col-md2">
                                        <div class="layui-form-item">
                                            <input type="checkbox"
                                                    {% if not item.already %} name="rules[]" {% endif %}
                                                   value="{{ key }},{{ item.name }},{{ item.path }}"
                                                   title="{{ item.name }}"
                                                   lay-filter="rule"
                                                    {% if item.name %} checked {% endif %}
                                                    {% if item.already %} disabled {% endif %}>
                                        </div>
                                        <div class="layui-word-aux">
                                            {{ item.path }}
                                        </div>
                                    </div>
                                {% endfor %}
                            </div>
                        </div>
                    </div>
                {% endfor %}

                <div class="plr30">
                    <div class="mtb20 text-center">
                        <button class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="save-rule">
                            保存
                        </button>

                        <button class="layui-btn layui-btn-primary layui-btn-sm">关闭</button>
                    </div>
                </div>
            </form>

        </div>
    </div>

{% endblock %}

{% block script %}
    <script type="text/javascript">
      layui.use(['http', 'toast', 'form'], function () {
        let form = layui.form
        let http = layui.http
        let toast = layui.toast

        form.on('submit(save-rule)', function (data) {
          let field = data.field

          http.post('{{ auth_route('develop/saveRule') }}', field, function (res) {
            toast.response(res)
          })

          return false
        })
      })
    </script>
{% endblock %}